package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.AreaMapper;
import com.gxa.insurance.pojo.Area;
import com.gxa.insurance.pojo.Driver;
import com.gxa.insurance.pojo.RepairDepot;
import com.gxa.insurance.pojo.S4Shop;
import com.gxa.insurance.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 */
@Service("areaService")
@Transactional(rollbackFor = Throwable.class)
public class AreaServiceImpl implements AreaService {
    @Autowired
    AreaMapper areaMapper;

    @Override
    public List<String> selectAll() throws Exception {
        List<String> areas = areaMapper.selectAll();
        if (areas.size() == 0) {
            throw new BusinessException(ExceptionCode.AREA_IS_EMPTY);
        }
        return areas;
    }

    @Override
    public List<List<S4Shop>> selectFourS(String areaName) {
        List<Area> s4Shops=areaMapper.selectAreaFourS(areaName);
//        List<String> names = new ArrayList<>();
        List<List<S4Shop>> lists=new ArrayList<>();
        for (int i = 0; i <s4Shops.size(); i++) {
            List<S4Shop> s4Shop = s4Shops.get(i).getS4Shop();
//            for (int j = 0; j < s4Shop.size(); j++) {
//                names.add(s4Shop.get(j).getName());
//            }
            lists.add(s4Shop);
        }
        return lists;
    }

    @Override
    public List<List<RepairDepot>> selectRepairFs(String areaName) {
        List<Area> repairDepots=areaMapper.selectAreaRepairF(areaName);
        List<List<RepairDepot>> lists = new ArrayList<>();
        for (int i = 0; i <repairDepots.size() ; i++) {
            List<RepairDepot> repairDepots1 = repairDepots.get(i).getRepairDepots();
//            for (int j = 0; j < repairDepots1.size(); j++) {
//                names.add(repairDepots1.get(j).getName());
//            }
            lists.add(repairDepots1);
        }
        return lists;
    }

    @Override
    public List<List<Driver>> selectDrivers(String areaName) {
        List<Area> drivers=areaMapper.selectDrivers(areaName);
        List<List<Driver>> lists = new ArrayList<>();
        for (int i = 0; i <drivers.size() ; i++) {
            List<Driver> drivers1 = drivers.get(i).getDrivers();
//            for (int j = 0; j < drivers1.size(); j++) {
//                names.add(drivers1.get(j).getName());
//            }
            lists.add(drivers1);
        }
        return lists;
    }
}
